package com.aspose.cells;

class zmg {
   com.aspose.cells.b.a.d.zm a;
   com.aspose.cells.b.a.d.zm b;
   private int c;
   private int d;
   private int e;
   private int f;
   private int g = 0;
   private int h = 0;
   private int i = 0;
   private byte[] j;

   void a(int var1, int var2) {
      int var3 = var1 - var2;
      this.c = (int)Math.ceil(com.aspose.cells.b.a.zq.a((double)var3, 2.0D));
      this.c = Math.max(this.c, 4);
      this.d = '\uffff' >> this.c;
      this.e = ~this.d;
      this.f = ('\uffff' >> this.c) + 3;
   }

   private int[] a(int var1) {
      int var2 = this.i;
      int var3 = 0;

      int var4;
      int var5;
      int var6;
      for(var4 = 0; var2 < this.h; ++var2) {
         var5 = var2;
         var6 = this.h;

         int var7;
         for(var7 = 0; var6 < var1 && this.j[var6] == this.j[var5]; ++var6) {
            ++var7;
            ++var5;
         }

         if (var7 > var3) {
            var3 = var7;
            var4 = var2;
         }
      }

      if (var3 >= 3) {
         this.a(this.h, this.i);
         var5 = Math.min(var3, this.f);
         var6 = this.h - var4;
         return new int[]{var6, var5};
      } else {
         return new int[]{0, 0};
      }
   }

   private int b(int var1, int var2) {
      this.a(this.h, this.i);
      int var3 = var1 - 1;
      int var4 = 16 - this.c;
      int var5 = var2 - 3;
      int var6 = var3 << var4 | var5;
      return var6;
   }

   private int a(int var1, int var2, int var3) {
      int var4 = var2 << var1;
      int var5 = var3 & ~var4;
      var3 = var5 | var4;
      return var3;
   }

   private int a(int var1, int var2, int var3, int var4) throws Exception {
      int[] var5 = this.a(var2);
      int var6 = var5[0];
      int var7 = var5[1];
      if (var6 != 0) {
         if (this.g + 1 < var1) {
            int var8 = this.b(var6, var7);
            this.a.b(com.aspose.cells.b.a.zc.a(var8), 0, 2);
            var4 = this.a(var3, 1, var4);
            this.g += 2;
            this.h += var7;
         } else {
            this.g = var1;
         }
      } else if (this.g < var1) {
         this.a.a(this.j[this.h]);
         ++this.g;
         ++this.h;
      } else {
         this.g = var1;
      }

      return var4;
   }

   void a(com.aspose.cells.b.a.d.zm var1, int var2) throws Exception {
      byte[] var3 = new byte[2];
      this.g = 0;
      this.h = 0;
      this.j = new byte[(int)(var1.h() - (long)var2)];
      var1.a((long)var2, 0);
      var1.a(this.j, 0, this.j.length);
      com.aspose.cells.b.a.d.zh var4 = new com.aspose.cells.b.a.d.zh();
      this.a = var4;
      var4.a((byte)1);
      ++this.g;
      int var5 = this.j.length;

      while(this.h < var5) {
         int var6 = this.g;
         this.i = this.h;
         int var7 = var6 + 4098;
         this.g = var6 + 2;
         var4.b(var3, 0, 2);

         int var8;
         int var9;
         int var10;
         int var11;
         for(var8 = Math.min(this.i + 4096, var5); this.h < var8 && this.g < var7; var4.m()[var9] = (byte)var10) {
            var9 = this.g;
            var10 = 0;
            var4.b(var3, 0, 1);
            ++this.g;

            for(var11 = 0; var11 <= 7; ++var11) {
               if (this.h < var8 && this.g < var7) {
                  var10 = this.a(var7, var8, var11, var10);
               }
            }
         }

         boolean var14 = false;
         byte var15;
         if (this.h >= var8) {
            var15 = 1;
         } else {
            var10 = var8 - 1;
            this.g = var6 + 2;
            var4.a((long)this.g, 0);
            this.h = this.i;
            short var16 = 4096;
            int var12 = var10 - this.h + 1;
            var4.b(this.j, this.h, var12);
            this.h += var12;
            this.g += var12;
            var11 = var16 - var12;

            for(int var13 = 0; var13 < var11; ++var13) {
               var4.a((byte)0);
            }

            this.g += var11;
            var15 = 0;
         }

         var10 = this.g - var6;
         var11 = var10 - 3;
         var11 |= var15 << 15;
         var11 |= 12288;
         var4.b();
         System.arraycopy(com.aspose.cells.b.a.zc.a(var11), 0, var4.m(), var6, 2);
      }

      var4.b();
   }

   void b(com.aspose.cells.b.a.d.zm var1, int var2) throws Exception {
      byte var3 = 0;
      int var4 = 0;
      byte[] var5 = new byte[(int)(var1.h() - (long)var2)];
      var1.a((long)var2, 0);
      var1.a(var5, 0, var5.length);
      if (var5[var3] != 1) {
         throw new Exception("Invalid VBA stream");
      } else {
         int var21 = var3 + 1;
         com.aspose.cells.b.a.d.zh var6 = new com.aspose.cells.b.a.d.zh();
         this.b = var6;

         while(true) {
            label39:
            while(var21 < var5.length) {
               int var8 = com.aspose.cells.b.a.zc.e(var5, var21) & '\uffff';
               int var9 = (var8 & 4095) + 3;
               boolean var10 = (var8 & '耀') != 0;
               int var11 = var4;
               int var12 = Math.min(var5.length, var21 + var9);
               var21 += 2;
               if (var10) {
                  while(true) {
                     byte var22;
                     do {
                        if (var21 >= var12) {
                           continue label39;
                        }

                        var22 = var5[var21];
                        ++var21;
                     } while(var21 >= var12);

                     for(int var14 = 0; var14 <= 7; ++var14) {
                        if (var21 < var12) {
                           if (((var22 & 255) >> var14 & 1) == 0) {
                              var6.b(var5, var21, 1);
                              ++var21;
                              ++var4;
                           } else {
                              this.a(var4, var11);
                              int var15 = com.aspose.cells.b.a.zc.e(var5, var21) & '\uffff';
                              int var16 = (var15 & this.d) + 3;
                              int var17 = var15 & this.e;
                              int var18 = 16 - this.c;
                              int var19 = (var17 >> var18) + 1;
                              int var20 = var4 - var19;
                              if (var16 > var19) {
                                 var6.b(var6.m(), var20, var19);

                                 while(var19 < var16) {
                                    var6.a(var6.m()[var20 + var19]);
                                    ++var19;
                                 }
                              } else {
                                 var6.b(var6.m(), var20, var16);
                              }

                              var4 += var16;
                              var21 += 2;
                           }
                        }
                     }
                  }
               } else {
                  short var13 = 4096;
                  var6.b(var5, var21, var13);
                  var21 += var13;
                  var4 += var13;
               }
            }

            return;
         }
      }
   }
}
